package com.hyyt.shopjsp.basic.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hyyt.shopjsp.util.common.StringStaticKey;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hyyt.shopjsp.basic.pojo.BasicSystemConfig;
import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicActorPurviewService;
import com.hyyt.shopjsp.basic.service.IBasicSystemConfigService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/3/17 0024下午 4:56.
 */
@Controller
@RequestMapping("/back/systemConfig")
public class BasicSystemConfigController extends BaseController {
	private Logger log = Logger.getLogger(BasicSystemConfigController.class);
	private String basePath = "/back/basic/systemConfig/";

	@Resource
	private IBasicSystemConfigService basicSystemConfigService;

	/**
	 * 进入系统配置管理
	 * @param param
	 * @return
	 */
	@RequestMapping("gotoSystemConfigPage")
	public String gotoSystemConfigPage(Map<String, Object> param){
		List<BasicSystemConfig> groupList = basicSystemConfigService.querySystemConfigGroupList(param);
        param.put("groupList", groupList);
		return basePath+"systemConfigList";
	}

	/**
	 * 查询所有系统配置
	 * @param page
	 * @param name			名称
	 * @param type			类型
	 * @param groupCode		分组类型
	 */
	@RequestMapping(value="listSystemConfig",method=RequestMethod.POST)
	@ResponseBody
	public void listSystemConfig(Page page,String groupCode,String name ,String type,HttpServletResponse response){
		Map<String, Object> param = new HashMap<>();
		if (Utils.objectIsNotEmpty(name)) {
			param.put("name", name.trim());
		}
		if (Utils.objectIsNotEmpty(type)) {
			param.put("type", type.trim());
		}
		if(Utils.objectIsNotEmpty(groupCode)){
			param.put("groupCode",groupCode);
		}
		param.put("sortColumns", " groupCode asc");
		int totalRecordCount =basicSystemConfigService.countBasicSystemConfig(param);
		 page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		 List<BasicSystemConfig> list =basicSystemConfigService.queryBasicSystemConfig(param, page.getPageRecordBeginIndex(), page.getPageSize());
		 param.clear();
	        param.put("total", totalRecordCount);// total键 存放总记录数，必须的
	        param.put("rows", list);// rows键 存放每页记录 list

	        ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
     * 根据用户id获得用户对象
     *
     * @param id
     */
    @RequestMapping(value = "getSystemConfigObject", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getSystemConfigObject(Integer id) {
        if (Utils.objectIsNotEmpty(id)) {
            BasicSystemConfig basicSystemconfig = basicSystemConfigService.loadBasicSystemConfig(id);
            return ResultMsg.create(true, null, FastJsonUtils.toJSONString(basicSystemconfig));
        }else {
			return ResultMsg.create(false);
		}
    }

    /**
     * 保存或者修改用户信息
     *
     * @param basicSystemconfig
     * @return
     */
    @RequestMapping(value = "saveOrUpdateSystemConfig", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateSystemConfig(BasicSystemConfig basicSystemconfig) {
        if (Utils.objectIsNotEmpty(basicSystemconfig)) {
            if (Utils.objectIsEmpty(basicSystemconfig.getId())) {
            	basicSystemConfigService.saveBasicSystemConfig(basicSystemconfig);
            } else {
            	basicSystemConfigService.updateBasicSystemConfig(basicSystemconfig);
            }
			return ResultMsg.create(true);
        }else{
			return ResultMsg.create(false);
		}

    }

    /**
     * 根据传入的id删除对象
     *
     * @param ids
     */
    @RequestMapping(value = "deleteSystemConfig", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteSystemConfig(String ids) {
        if (Utils.objectIsNotEmpty(ids)) {
        	basicSystemConfigService.deleteBasicSystemConfig(Integer.parseInt(ids));
			return ResultMsg.create(true);
        }else{
			return ResultMsg.create(false);
		}
    }

    /**
     * 更新初始化配置
     * @param response
     */
    @RequestMapping(value="updateSystemConfig",method=RequestMethod.POST)
    @ResponseBody
    public void updateSystemConfig(HttpServletResponse response){
    	Map<String, Object> param=new HashMap<>();
    	  //声明一个变量result 默认为false  当字典初始化成功时 将其更改为true
        boolean success = false;
        //查询要加载的数据（通过type进行了去重）
        List<BasicSystemConfig> systemConfigList = basicSystemConfigService.initSystemConfigData();
        if (Utils.collectionIsNotEmpty(systemConfigList)) {
            for (BasicSystemConfig sys : systemConfigList) {
                //通过type查询对应的集合
                redisBasicService.setKey(StringStaticKey.SYSTEMCONFIG_PREFIX + sys.getType(), sys.getValue());
            }
            //为静态map对象赋值
            success = true;//查询到数据 将result更改为true
            param.put("success", success);
        }
        ResultMsg.create(param, response);  //把内容发送到页面
    }

	/**
	 * 进入同步数据页面
	 * @return
	 */
	@RequestMapping("gotoSyncDataPage")
	public String gotoSyncDataPage(){
		return basePath+"syncDataList";
	}

	/**
	 * 查询出需要初始化数据的列表
	 * @param page
	 * @param response
	 * @param request
	 */
	@RequestMapping(value = "syncData",method = RequestMethod.POST)
    @ResponseBody
	public void syncData(Page page, HttpServletResponse response, HttpServletRequest request){
		String name=request.getParameter("name");//名称
		String type=request.getParameter("type");//类型名称
		Map<String, Object> param = new HashMap<>();
		if (Utils.objectIsNotEmpty(name)) {//是否检索名称
			param.put("name", name.trim());
		}
		param.put("groupCode", "syncSysDataPath"); //查询需要初始化的type
		param.put("sortColumns", "id desc");
		int totalRecordCount =basicSystemConfigService.countBasicSystemConfig(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<BasicSystemConfig> list =basicSystemConfigService.queryBasicSystemConfig(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", list);// rows键 存放每页记录 list

		ResultMsg.create(param, response);  //把内容发送到页面
	}

}